package com.shop.controller;

import com.shop.entity.BrandCountChart;
import com.shop.entity.BrandPriceChart;
import com.shop.entity.Res;
import com.shop.service.PhoneService;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("chart")
public class ChartController {

    @Autowired
    PhoneService phoneService;

    @GetMapping("brandSaleCount")
    public Res brandSaleCount () {
        List<BrandCountChart> list = phoneService.getBrandCount();
        List<String > brands = new ArrayList<>(list.size());
        List<Integer > nums = new ArrayList<>(list.size());
        list.forEach(v -> {
            brands.add(v.getBrandName());
            nums.add(v.getNum());
        });
        HashMap<String ,Object> params = new HashMap<>();

        params.put("brands",brands);
        params.put("nums",nums);
        return Res.success(params);
    }

/*    @GetMapping("brandPriceCount")
    public Res brandPriceCount() {
        List<BrandPriceChart> list = phoneService.getBrandPriceCount();
        List<String > brands = new ArrayList<>(list.size());
        List<Float > prices = new ArrayList<>(list.size());
        list.forEach(v -> {
            brands.add(v.getBrandName());
            prices.add(v.getPrice());
        });
        HashMap<String ,Object> params = new HashMap<>();

        params.put("brands",brands);
        params.put("prices",prices);
        return Res.success(params);
    }*/

@GetMapping("brandPriceCount")
public Res brandPriceCount() {
    List<BrandPriceChart> list = phoneService.getBrandPriceCount();
    @Data
    @AllArgsConstructor
    class KV {
        String name;
        Float value;
    }
    List<KV> list1  = new ArrayList<>();
    list.forEach(x -> {
        KV kv = new KV(x.getBrandName(), x.getPrice());

        list1.add(kv);
    });
    return Res.success(list1);
}
}
